          <?php $errorLinkage = Yii::app()->user->hasFlash('errorLinkage') ? unserialize(Yii::app()->user->getFlash('errorLinkage')) : null ?>
          <?php $errorQuestion = Yii::app()->user->hasFlash('errorQuestion') ? unserialize(Yii::app()->user->getFlash('errorQuestion')) : null ?>
          <?php $errorAnswer = Yii::app()->user->hasFlash('errorAnswer') ? unserialize(Yii::app()->user->getFlash('errorAnswer')) : null ?>
          <!-- START OF MAINCOL -->
          <div class="MainCol AddEbookPage">
          	  <h1>编辑心理测试题目</h1>
              <div class="right"><a href="#add_question" class="Blue_btn" title="add new ebook"><span><?= CHtml::image(Yii::app()->baseUrl.'/images/add_icon.gif', '', array('class' => 'icon')); ?> 添加新题目</span></a>
              </div>
              <span class="NormalFont SmallFont">
                  <?= CHtml::image(Yii::app()->baseUrl.'/images/home_icon.gif', '主页'); ?> > 心理测试管理 > 编辑心理测试题目
               </span><br /><br />
              <div class="Titlebox box"><!-- DON'T DELETE --></div>

                <h4 id="add_dim">“<?php echo htmlspecialchars($topic->topicTitle) ?>”的题目</h4>
                
              <?php if(Yii::app()->user->hasFlash('successMessage')) : ?>
              <div class="SuccessfulBox Smallbox">
                <span class="Successful"> <?php echo Yii::app()->user->getFlash('successMessage'); ?></span>
              </div>
              <?php endif; ?>

			<?php if(count($questions) > 0) : ?>
			<?php foreach($questions as $k => $question) : ?>
              <div class="Smallbox" id="question_<?php echo $question->questionId ?>"><label>题目序号</label>
                  <span class="question-order" id="question_order_<?php echo $question->questionId ?>"><?php echo $question->questionOrder ?></span>
              </div>
              
              <div class="Smallbox"><label>题目</label>
                  <span class="question" id="question_question_<?php echo $question->questionId ?>"><?php echo $question->question ?></span>
                  <?php echo CHtml::link('&nbsp;', array('/admin/psytest/deletequestion', 'id' => $question->questionId), array('style' => 'float: right; margin-top: -18px;', 'class' => 'Delete', 'confirm' => '确定要删除该题目吗？')) ?>
              </div>
              
              <?php if(count($question->answers) > 0) : ?>
              <div class="Smallbox"><label>答案选项</label>
              	<ul class="Lists">
              		<?php foreach($question->answers as $answer) : ?>
              		<li id="answer_lists_<?php echo $answer->answerId ?>">
              			<span class="ContentBox">
              			<span class="answer-order" id="answer_order_<?php echo $answer->answerId ?>"><?php echo $answer->answerOrder ?></span>. <span class="answer" id="answer_<?php echo $answer->answerId?>"><?php echo $answer->answer ?></span>
              			</span>
              			<?php echo CHtml::link('&nbsp;', array('/admin/psytest/deleteanswer', 'id' => $answer->answerId), array('style' => 'float: right; margin-top: -18px;', 'class' => 'Delete', 'confirm' => '确定要删除该答案选项吗？')) ?>
                        <?php if($answer->linkage === null) : ?>
                        <a href="#" class="toggle-linkage-form">添加跳转目标</a>
                        <span <?php if( ! ($errorLinkage !== null && $errorLinkage->answerId == $answer->answerId)) : ?>
style="display: none"<?php endif ?>>
                        <?php if($errorLinkage !== null && $errorLinkage->answerId == $answer->answerId) : ?>
                        <?php echo CHtml::errorSummary($errorLinkage); ?>
                        <?php endif ?>
                        <?php echo CHtml::form(array('/admin/psytest/addlinkage')); $_linkage->answerId = $answer->answerId ?>
                        <?php echo CHtml::activeHiddenField($_linkage, 'answerId') ?>
                        跳转目标类型：<?php echo CHtml::activeDropDownList($_linkage, 'linkType', TestAnswerLinkage::linkType2Text(), array('class' => 'edit-jumping-to-type')) ?><br />
                        跳转目标：<?php echo CHtml::activeDropDownList($_linkage, 'linkToId', $questionsListData, array('class' => 'edit-link-to-id-question')) ?><?php echo CHtml::activeDropDownList($_linkage, 'linkToId', CHtml::listData($conclusions, 'conclusionId', 'conclusionSum'), array('class' => 'edit-link-to-id-conclusions')) ?><br />
                        <?php echo CHtml::submitButton('提交') ?>
                        <?php echo CHtml::endForm() ?>
                        <?php else : ?>
                        跳转目标类型：<span class="link-type" id="link_type_<?php echo $answer->linkage->linkageId ?>"><?php echo $answer->linkage->getTypeText() ?></span><br />
                        跳转目标：<span class="link-to" id="link_to_<?php echo $answer->linkage->linkageId ?>"><?php if($answer->linkage->linkType == TestAnswerLinkage::LINKTYPE_QUESTION) : ?><?php echo $answer->linkage->question->questionOrder ?>. <?php echo $answer->linkage->question->question ?><?php elseif($answer->linkage->linkType == TestAnswerLinkage::LINKTYPE_CONCLUSION) : ?><?php echo $answer->linkage->conclusion->conclusionSum ?></span><?php endif ?>
                        <br /><a href="<?php echo CHtml::normalizeUrl(array('/admin/psytest/editanswerlinkage', 'id' => $answer->linkage->linkageId)) ?>">编辑跳转</a>
                        <?php endif ?>
                        </span>
              		</li>
              		<?php endforeach ?>
              	</ul>
              </div>
              <?php endif ?>
              <div class="Smallbox"><label>&nbsp;</label>
				<a title="添加答案选项" class="toggle-answer-form Grey_btn" href=""><span>添加答案选项</span></a>&nbsp;
              	<?php --$k; if(isset($questions[$k]) && $questions[$k] !== null) : ?> 或者<?php echo CHtml::link('复制上题答案', array('/admin/psytest/copyanswers', 'from' => $questions[$k]->questionId, 'to' => $question->questionId), array('confirm' => '确定要复制答案吗？')) ?><?php endif ?>
              </div>
              <div <?php if( ! ($errorAnswer !== null && $errorAnswer->questionId == $errorAnswer->questionId)) : ?>
style="display: none"<?php endif ?>>
                <?php echo CHtml::form(array('/admin/psytest/addanswer')); $_answer->questionId = $question->questionId ?>
                <?php echo CHtml::activeHiddenField($_answer, 'questionId') ?>
                <?php if($errorAnswer !== null && $errorAnswer->questionId == $question->questionId) : ?>
                <?php echo CHtml::errorSummary($errorAnswer); ?>
                <?php endif ?>
              	<div class="Smallbox"><label>&nbsp;</label>
                       	 选项序号<span class="Red">*</span> <?php echo CHtml::activeTextfield($_answer, 'answerOrder', array('class' => 'TxtInput')) ?><br />
                     	 选项描述<span class="Red">*</span> <?php echo CHtml::activeTextarea($_answer, 'answer', array('class' => 'TxtInput')) ?><br />
                </div>
              	<div class="Smallbox"><label>&nbsp;</label>
                <?php echo CHtml::submitButton('提交') ?>
                <?php echo CHtml::endForm() ?>
                </div>
              </div>
            <?php endforeach ?>
            <?php endif ?>
              <!--<div class="Midbox Red SmallFont Smallbox"><label>&nbsp;</label>仅描述可以编辑，单击其文字进入编辑状态。</div>-->
            <hr />
            <h3 id="add_question">添加新题目</h3>
            <?php if($errorQuestion !== null) : ?>
            <?php echo CHtml::errorSummary($errorQuestion) ?>
            <?php endif ?>
            <?php echo CHtml::form(array('/admin/psytest/addquestion')); $_question->topicId = $topic->topicId ?>
            <?php echo CHtml::activeHiddenField($_question, 'topicId') ?>
            <div class="Smallbox"><label>题目序号<span class="Red">*</span></label>
            <?php echo CHtml::activeTextField($_question, 'questionOrder', array('class' => 'TxtInput', 'value' => isset($question) && $question !== null ? $question->questionOrder+1 : 1)) ?>
            </div>
            <div class="Smallbox"><label>题目<span class="Red">*</span></label>
            <?php echo CHtml::activeTextArea($_question, 'question') ?>
            </div>
            <div class="Smallbox"><label>&nbsp;</span></label>
            <?php echo CHtml::submitButton('提交') ?>
            </div>
              
              <div class="Smallbox btnBox"><label>&nbsp;</label>
              <?= CHtml::link('返回', array('admin/psytest/edittopic', 'id' => $topic->topicId)); ?>
              </div>
              <?php echo CHtml::endForm() ?>
          </div>
          <!-- END OF MAINCOL -->
<script type="text/javascript">
$(document).ready(function() {
    $('a.toggle-linkage-form').toggle(function() {
        $(this).next('span').show();
    }, function() {
        $(this).next('span').hide();
    });
    $('a.toggle-answer-form').toggle(function() {
        $(this).parent('div').next('div').show();
    }, function() {
        $(this).parent('div').next('div').hide();
    });
    $('a.toggle-dim-form').toggle(function() {
        $(this).next('span').show();
    }, function() {
        $(this).next('span').hide();
    });

    $('span.question-order').editable('<?php echo $this->createAbsoluteUrl('/admin/psytest/updatequestionorder') ?>', {
        type      : 'text',
        cancel    : '取消',
        submit    : '确定',
        width	  : '120px',
        indicator : '<img src="<?php echo Yii::app()->baseUrl.'/images/ajax-loader.gif' ?>">',
        tooltip   : '单击进行编辑...'
    });

    $('span.question').editable('<?php echo $this->createAbsoluteUrl('/admin/psytest/updatequestion') ?>', {
        type      : 'textarea',
        cancel    : '取消',
        submit    : '确定',
		width	   : '360px',
		 height    : '72px',
        indicator : '<img src="<?php echo Yii::app()->baseUrl.'/images/ajax-loader.gif' ?>">',
        tooltip   : '单击进行编辑...'
    });
    
	$('span.answer').editable('<?php echo $this->createAbsoluteUrl('/admin/psytest/updateanswer') ?>', {
         type      : 'textarea',
         cancel    : '取消',
		 width	   : '340px',
		 height    : '72px',
         submit    : '确定',
         indicator : '<img src="<?php echo Yii::app()->baseUrl.'/images/ajax-loader.gif' ?>">',
         tooltip   : '单击进行编辑...'
     });
     $('span.answer-order').editable('<?php echo $this->createAbsoluteUrl('/admin/psytest/updateanswerorder') ?>', {
         type      : 'text',
         cancel    : '取消',
		 width	   : '60px',
         submit    : '确定',
         indicator : '<img src="<?php echo Yii::app()->baseUrl.'/images/ajax-loader.gif' ?>">',
         tooltip   : '单击进行编辑...'
     });
     $('select.edit-jumping-to-type').change(function() {
         if($(this).val() == '<?php echo TestAnswerLinkage::LINKTYPE_QUESTION ?>') {
            $(this).siblings('select.edit-link-to-id-conclusions').hide();
            $(this).siblings('select.edit-link-to-id-conclusions').attr('disabled', true);
            $(this).siblings('select.edit-link-to-id-question').show();
            $(this).siblings('select.edit-link-to-id-question').attr('disabled', '');
         } else if($(this).val() == '<?php echo TestAnswerLinkage::LINKTYPE_CONCLUSION ?>') {
            $(this).siblings('select.edit-link-to-id-question').hide();
            $(this).siblings('select.edit-link-to-id-question').attr('disabled', true);
            $(this).siblings('select.edit-link-to-id-conclusions').show();
            $(this).siblings('select.edit-link-to-id-conclusions').attr('disabled', '');
         }
     });
     $('.edit-jumping-to-type').trigger('change');
});
</script>
